package com.xiaode.controller.admin.account;

import com.jfinal.core.Const;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;
import com.xiaode.exce.EnumError;
import com.xiaode.validator.BaseValidator;

import java.io.File;
import java.util.List;

public class AccountFileValidator extends BaseValidator {
    @Override
    protected void validate(Controller c) {
        //设置短路验证,有参数校验失败则返回
        setShortCircuit(true);
        String url = getActionKey();
        //如果是导入接口 判断附件大小与格式
        if ("/aAccount/importStudent".equals(url) || "/aAccount/importTeacher".equals(url)) {
            List<UploadFile> files = null;
            try {
                files = c.getFiles();
            } catch (Exception e) {
                setRet(Ret.fail().set("error", EnumError.EXCEL_NOT_EXIST.getObj()));
                addError("error", EnumError.EXCEL_NOT_EXIST.getObj().toJson());
            }
            try {
                Boolean isUpdateExist = c.getParaToBoolean("isUpdateExist");
                if (isUpdateExist == null){
                    setRet(Ret.fail().set("error", EnumError.IS_UPDATE_EXIST_NULL.getObj()));
                    addError("error",EnumError.IS_UPDATE_EXIST_NULL.getObj().toJson());
                }
                UploadFile file = files.get(0);
                String fileName = file.getFileName();
                if (!fileName.endsWith(".xls") && !fileName.endsWith(".xlsx")) {
                    file.getFile().delete();
                    setRet(Ret.fail().set("error", EnumError.EXCEL_INCORRECT_FORMAT.getObj()));
                    addError("error", EnumError.EXCEL_INCORRECT_FORMAT.getObj().toJson());
                }
                long length = file.getFile().length();
                //校验文件大小
                if (length > Const.DEFAULT_MAX_POST_SIZE) {
                    file.getFile().delete();
                    setRet(Ret.fail().set("error", EnumError.FILE_TOO_BIG.getObj()));
                    addError("error", EnumError.FILE_TOO_BIG.getObj().toJson());
                }
            } catch (Exception e) {
                //抛出异常则 删除附件
                for (UploadFile file : files) {
                    File ufs = file.getFile();
                    ufs.delete();
                }
                c.renderJson(Ret.fail().set("error", EnumError.SERVICE_FAIL.getObj()));
                return;
            }
        }
        //校验密码是否符合规范
        if ("/aAccount/editAdmin".equals(url)) {
            String password = c.getPara("password");
            String rul = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$";
            if (password != null && !password.matches(rul)) {
                setRet(Ret.fail().set("error", EnumError.PASSWORD_INCONFORMITY.getObj()));
                addError("error", EnumError.PASSWORD_INCONFORMITY.getObj().toJson());
            }
        }
        //校验number长度是否符合
        if ("/aAccount/editStudent".equals(url)){
            String number = c.getPara("number");
            if (number != null && number.length() > 5){
                setRet(Ret.fail().set("error", EnumError.NUMBER_TOO_LONG.getObj()));
                addError("error", EnumError.NUMBER_TOO_LONG.getObj().toJson());
            }
        }
        //校验account number
        if ("/account/addStudent".equals(url)){
            String number = c.getPara("number");
            String account = c.getPara("account");
            String rex="/[^\\d]/g";
            if (!number.matches(rex) || !account.matches(rex)) {
                Kv kv = Kv.by("msg", "学籍号与学号只允许为数字！").set("code", 3001);
                setRet(Ret.fail().set("error", kv));
                addError("error", kv.toJson());
            }
        }
    }

    @Override
    protected void handleError(Controller c) {
        c.renderJson(getRet());
    }
}
